<?php

//定时任务相关控制器

namespace hotel\modules\api\controllers;

use hotel\common\models\Question;
use hotel\common\models\QuestionType;
use hotel\common\models\Opinion;
use hotel\common\utils\Tool;
use Yii;
use yii\log\FileTarget;


class QuestionController extends CoreController
{
    //获取问答标签
    Public function actionGetQuestionType()
    {
        $data = QuestionType::find()->asArray()->all();
        foreach ($data as $k => $v) {
            $type_id = $v['id'];
            $count = Question::find()->where("type_id = $type_id")->count();
            $data[$k]['num'] = $count;
        }
        $all_count = Question::find()->count();

        $arr = [
            'id' => 0,
            'title' => '全部',
            'num' => $all_count
        ];
        array_unshift($data, $arr);
        return Tool::showRes([0, '获取问答标签成功'], $data);
    }


    //根据类型获取问答
    public function actionGetQuestionByTypeId()
    {
        $request = \Yii::$app->request;
        $post_data = $request->post();
        $search = isset($post_data['search']) ? $post_data['search'] : '';
        $type_id = isset($post_data['type_id']) ? $post_data['type_id'] : 0;
        $page = isset($post_data['page']) ? $post_data['page'] : 1;
        $limit = isset($post_data['limit']) ? $post_data['limit'] : 10;
        $query = Question::find();
        if ($type_id != 0) {
            $query->andWhere(['=', 'type_id', $type_id]);
        }
        $query->andFilterWhere(['like', 'title', $search]);
        $pages = $query->count();
        $data = $query
            ->offset($limit * ($page - 1))
            ->orderBy("id desc")
            ->limit($limit)
            ->asArray()->all();

        $res['list'] = $data;
        $res['pageInfo'] = [
            'count' => $pages,
            'pageSize' => $limit,
            'currPage' => $page,
            'tatalPage' => ceil($pages / $limit),
        ];
        return Tool::showRes([0, '获取问答成功'], $res);
    }


    //提问
    public function actionAddOpinion(){
        $request = \Yii::$app->request;
        $post_data = $request->post();
        $appid = $post_data['appid'];
        $title = $post_data['title'];
        $member_id = $this->member_id;
        $model = New Opinion();
        $model->appid = $appid;
        $model->title = $title;
        $model->member_id = $member_id;
        $model->create_time = date('Y-m-d h:i:s');
        if ($model->save()) {
            return Tool::showRes([0, '记录成功']);
        } else {
            return Tool::showRes([400, '记录失败']);
        }
    }

    //我的提问
    public function actionMyOpinion(){
        $member_id = $this->member_id;
        $data = Opinion::find()->where("member_id = $member_id")->asArray()->all();
        return Tool::showRes([0, '获取我的问答成功'], $data);

    }

}
